import { defineStore } from "pinia";
import { EnumOpType, EnumPlayOrPauseSubType, IOpType, IViewOp } from "@renderer/assets/types";
import { useViewManagerStore } from "@renderer/store/pinia/viewManager";


const useUserOpStore = defineStore("userOp", {
  state: () => {
    return {
      viewOpObj: {} as IViewOp, // viewId 所对应的 opButtons
    };
  },
  getters: {
  },
  actions: {
    addNewViewOpObjByTabId(tabId) {
      const viewStore = useViewManagerStore()
      const viewArr: any[] = viewStore.tabSubView[tabId];
      viewArr.forEach((view) => {
        this.viewOpObj[view.viewId] = {
          activeOp: {
            type: EnumOpType.pan,
            subType: "",
          },
          playSpeed: 100,
          disabledOpList: [],
        };
      })
    },
    addGroupNewViewOpObjByTabId(tabId) {
      const viewStore = useViewManagerStore()
      const viewArr: any[] = viewStore.tabSubView[tabId];
      this.viewOpObj[viewArr[0].groupId] = {
        activeOp: {
          type: EnumOpType.pan,
          subType: "",
        },
        playSpeed: 100,
        disabledOpList: [],
      };
    },
    createNewViewOpObj(viewId: string){
      if (!this.viewOpObj[viewId]) {
        this.viewOpObj[viewId] = {
          activeOp: {
            type: EnumOpType.pan,
            subType: "",
          },
          playSpeed: 100,
          disabledOpList: [],
        };
      }
    },
    removeViewOpByTab(tabId) {
      const viewStore = useViewManagerStore()
      const viewArr: any[] | undefined = viewStore.tabSubView[tabId];
      if (viewArr){
        viewArr.forEach((view) => {
          delete this.viewOpObj[view.viewId];
        })
      }
    },
    setDefaultActiveOp(viewId) {
      this.clearDisable(viewId);
      this.viewOpObj[viewId].activeOp = {
        type: EnumOpType.pan,
        subType: "",
      };
    },
    setDisableWhilePlaying(viewId) {
      this.viewOpObj[viewId].disabledOpList = [
        EnumOpType.pan,
        EnumOpType.zoom,
        EnumOpType.window,
        EnumOpType.rotate,
        EnumOpType.reset,
        EnumOpType.scroll,
        EnumOpType.measure,
      ];
    },
    clearDisable(viewId) {
      this.viewOpObj[viewId].disabledOpList = [];
    },
    setActiveOp(viewId: string, activeOp: IOpType) {

      this.viewOpObj[viewId].activeOp = activeOp;
      switch (activeOp.type) {
        case EnumOpType.playOrPause:
          if (activeOp.subType == EnumPlayOrPauseSubType.play) {
            this.setDisableWhilePlaying(viewId);
          }else if (activeOp.subType == EnumPlayOrPauseSubType.pause) {
            this.setDisableWhilePlaying(viewId);
          }else if (activeOp.subType == EnumPlayOrPauseSubType.end) {
            this.clearDisable(viewId);
          }
          break;
        case EnumOpType.rotate:
          this.viewOpObj[viewId].activeOp = activeOp;
          setTimeout(() => {
            this.setDefaultActiveOp(viewId);
          }, 300);
          break;

        case EnumOpType.reset:
          this.viewOpObj[viewId].activeOp = activeOp;
          setTimeout(() => {
            this.setDefaultActiveOp(viewId);
          }, 300);
          break;
        default:
          this.viewOpObj[viewId].activeOp = activeOp;
          break;
      }
    },
    setPlaySpeed(viewId, playSpeed: any) {
      this.viewOpObj[viewId].playSpeed = playSpeed;
    },
  },
});

export { useUserOpStore };
